﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web

Public Class SaleModifyTPZKSP
    Inherits BusinessDomainBase

    Private _businessGUID As String     '销售变更申请GUID
    Private _saleModiSP As SaleModiSP   '销售变更审批
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/slxt/SaleModifyTPZKSP.xml")
        Me._xml.LoadXml(MyBase.XML)
        Me._saleModiSP = New SaleModiSP()
    End Sub

    '属性：销售变更申请GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return Me._businessGUID
        End Get
        Set(ByVal Value As String)
            Me._businessGUID = Value
            Me._saleModiSP.BusinessGUID = Me._businessGUID
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return Me._xml.OuterXml
        End Get
        Set(ByVal Value As String)
            Me._xml.LoadXml(Value)
        End Set
    End Property

    '功能：取销售变更申请数据，填充XML
    Public Overrides Function LoadData() As Integer
        Dim strSQL As String
        Dim i, j, iRowCount As Integer
        Dim dtTemp As DataTable
        '主记录    
        strSQL = "SELECT * FROM " & _
                "(SELECT SMA.SaleModiApplyGUID, SMA.SaleGUID, SMA.SaleType, SMA.ApplyType, " & _
                "SMA.ReasonSort, SMA.Reason, SMA.ApplyBy, CONVERT(VARCHAR(10),SMA.ApplyDate,121) AS ApplyDate, SMA.BUGUID, " & _
                "SMA.ProjGUID,PJ.ProjName, EPR.RoomGUID, EPR.RoomInfo,EOR.Ywy, " & _
                "EOR.BldArea, EOR.TnArea, EOR.CalMode,EOR.CstName,EOR.CstAllGUID,'认购' AS SaleStatus, " & _
                "CONVERT(VARCHAR(10),EOR.QSDate,121) AS RgDate,NULL AS QyDate,SMA.ApproveState,EPR.Price,EPR.TnPrice,EPR.Total, " & _
                "EOR.BldCjPrice,EOR.TnCjPrice,EOR.RoomTotal as CjTotal," & _
                "SMI.PayformName,EOR.DiscntValue,SMI.RoomTotal,SMI.RmbCjTotal,SMI.OfferType,SMI.OfferDiscntValue, " & _
                "SMI.OfferPrice, SMI.BldCjPrice as NewBldCjPrice, SMI.TnCjPrice as NewTnCjPrice, SMI.RoomTotal as NewCjTotal, SMI.DjBldCjPrice,SMI.DjTnCjPrice,SMI.DjRoomTotal " & _
                "FROM s_SaleModiApply SMA " & _
                "LEFT JOIN s_SModiInfo SMI ON SMA.SaleModiApplyGUID = SMI.SaleModiApplyGUID " & _
                "LEFT JOIN es_Order EOR ON SMA.SaleGUID = EOR.OrderGUID " & _
                "LEFT JOIN ep_room EPR ON SMA.RoomGUID = EPR.RoomGUID " & _
                "LEFT JOIN p_Project PJ ON PJ.ProjGUID = SMA.ProjGUID " & _
                "WHERE SaleType='定单' " & _
                        "UNION ALL " & _
                "SELECT SMA.SaleModiApplyGUID, SMA.SaleGUID, SMA.SaleType, SMA.ApplyType, " & _
                "SMA.ReasonSort, SMA.Reason, SMA.ApplyBy, CONVERT(VARCHAR(10),SMA.ApplyDate,121) AS ApplyDate, SMA.BUGUID, " & _
                "SMA.ProjGUID,PJ.ProjName, EPR.RoomGUID, EPR.RoomInfo,EOR.Ywy, " & _
                "EOR.BldArea, EOR.TnArea, EOR.CalMode,EOR.CstName,EOR.CstAllGUID,'签约' AS SaleStatus, " & _
                "CONVERT(VARCHAR(10),OT.QSDate,121) AS RgDate,CONVERT(VARCHAR(10),EOR.QSDate,121) AS QyDate,SMA.ApproveState,EPR.Price,EPR.TnPrice,EPR.Total, " & _
                "EOR.BldCjPrice,EOR.TnCjPrice,EOR.RoomTotal as CjTotal," & _
                "SMI.PayformName,EOR.DiscntValue,SMI.RoomTotal,SMI.RmbCjTotal,SMI.OfferType,SMI.OfferDiscntValue, " & _
                "SMI.OfferPrice, SMI.BldCjPrice as NewBldCjPrice, SMI.TnCjPrice as NewTnCjPrice, SMI.RoomTotal as NewCjTotal, SMI.DjBldCjPrice, SMI.DjTnCjPrice, SMI.DjRoomTotal " & _
                "FROM s_SaleModiApply SMA " & _
                "LEFT JOIN s_SModiInfo SMI ON SMA.SaleModiApplyGUID = SMI.SaleModiApplyGUID " & _
                "LEFT JOIN es_Contract EOR ON SMA.SaleGUID = EOR.ContractGUID " & _
                "LEFT JOIN ep_room EPR ON SMA.RoomGUID = EPR.RoomGUID " & _
                "LEFT JOIN p_Project PJ ON PJ.ProjGUID = SMA.ProjGUID " & _
                "LEFT JOIN (SELECT s_Trade.TradeGUID,s_Order.QSDate FROM s_Trade INNER JOIN s_Order ON s_Order.OrderGUID = s_Trade.RGOrderGUID) OT ON OT.TradeGUID = EOR.TradeGUID " & _
                "WHERE SaleType='合同' ) TEMP " & _
                "WHERE SaleModiApplyGUID='" & Me._businessGUID & "'"



        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then

            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID

            '主记录
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售变更申请GUID']").InnerText = dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单GUID']").InnerText = dtTemp.Rows(0).Item("SaleGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单类型']").InnerText = dtTemp.Rows(0).Item("SaleType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请类型']").InnerText = dtTemp.Rows(0).Item("ApplyType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因分类']").InnerText = dtTemp.Rows(0).Item("ReasonSort").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因说明']").InnerText = dtTemp.Rows(0).Item("Reason").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请人']").InnerText = dtTemp.Rows(0).Item("ApplyBy").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请日期']").InnerText = dtTemp.Rows(0).Item("ApplyDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='单位GUID']").InnerText = dtTemp.Rows(0).Item("BUGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目GUID']").InnerText = dtTemp.Rows(0).Item("ProjGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目名称']").InnerText = dtTemp.Rows(0).Item("ProjName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间GUID']").InnerText = dtTemp.Rows(0).Item("RoomGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间号']").InnerText = dtTemp.Rows(0).Item("RoomInfo").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='建筑单价']").InnerText = dtTemp.Rows(0).Item("Price").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='套内单价']").InnerText = dtTemp.Rows(0).Item("TnPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='标准总价']").InnerText = dtTemp.Rows(0).Item("Total").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='业务员']").InnerText = dtTemp.Rows(0).Item("Ywy").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='建筑面积']").InnerText = dtTemp.Rows(0).Item("BldArea").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='套内面积']").InnerText = dtTemp.Rows(0).Item("TnArea").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='计价方式']").InnerText = dtTemp.Rows(0).Item("CalMode").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户姓名']").InnerText = dtTemp.Rows(0).Item("CstName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户GUID']").InnerText = dtTemp.Rows(0).Item("CstAllGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售状态']").InnerText = dtTemp.Rows(0).Item("SaleStatus").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='认购日期']").InnerText = dtTemp.Rows(0).Item("RgDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='签约日期']").InnerText = dtTemp.Rows(0).Item("QyDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原建筑成交单价']").InnerText = dtTemp.Rows(0).Item("BldCjPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原套内成交单价']").InnerText = dtTemp.Rows(0).Item("TnCjPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原成交房间总价']").InnerText = dtTemp.Rows(0).Item("CjTotal").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='审批状态']").InnerText = dtTemp.Rows(0).Item("ApproveState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='付款方式']").InnerText = dtTemp.Rows(0).Item("PayformName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='折扣']").InnerText = dtTemp.Rows(0).Item("DiscntValue").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='协议总价(人民币)']").InnerText = dtTemp.Rows(0).Item("RmbCjTotal").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='优惠方式']").InnerText = dtTemp.Rows(0).Item("OfferType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='优惠折扣']").InnerText = dtTemp.Rows(0).Item("OfferDiscntValue").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='优惠金额']").InnerText = dtTemp.Rows(0).Item("OfferPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='新建筑成交单价']").InnerText = dtTemp.Rows(0).Item("NewBldCjPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='新套内成交单价']").InnerText = dtTemp.Rows(0).Item("NewTnCjPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='新成交房间总价']").InnerText = dtTemp.Rows(0).Item("NewCjTotal").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='底价建筑成交单价']").InnerText = dtTemp.Rows(0).Item("DjBldCjPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='底价套内成交单价']").InnerText = dtTemp.Rows(0).Item("DjTnCjPrice").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='底价房间总价']").InnerText = dtTemp.Rows(0).Item("DjRoomTotal").ToString

            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='新建筑成交单价与建筑底价单价差异']").InnerText = Single.Parse(dtTemp.Rows(0).Item("NewBldCjPrice").ToString) - Single.Parse(dtTemp.Rows(0).Item("DjBldCjPrice").ToString)
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='新套内成交单价与套内底价单价差异']").InnerText = Single.Parse(dtTemp.Rows(0).Item("NewTnCjPrice").ToString) - Single.Parse(dtTemp.Rows(0).Item("DjTnCjPrice").ToString)
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='新房间总价与底价总价差异']").InnerText = Single.Parse(dtTemp.Rows(0).Item("NewCjTotal").ToString) - Single.Parse(dtTemp.Rows(0).Item("DjRoomTotal").ToString)


        End If

        Return 0

    End Function

    '功能：发起“销售变更”的审批流程
    Public Overrides Function Initiate() As String

        Return Me._saleModiSP.Initiate()

    End Function

    '功能：根据XML进行数据对“销售变更”进行归档
    Public Overrides Function SaveData() As String

        Return Me._saleModiSP.SaveData()

    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String

        Return Me._saleModiSP.SaveData_NoPass()

    End Function

    '功能：审批作废处理
    Public Overrides Function SaveData_Cancel() As String

        Return Me._saleModiSP.SaveData_Cancel()

    End Function

    '功能：发起撤回“销售变更”的审批流程
    Public Overrides Function InitiateWithDrawn() As String

        '由于发起撤回的审批流程和审批作废处理是一样的，所以直接调用审批作废的方法即可
        Return Me._saleModiSP.InitiateWithDrawn()

    End Function

End Class
